<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Website extends Admin_Controller
{
	function Website()
	{
		parent::Admin_Controller();

		$this->load->helper('form');

		// Load userlib language
		$this->lang->load('userlib');

		// Set breadcrumb
		$this->bep_site->set_crumb($this->lang->line('backendpro_members'),'auth/admin/members');

		// Check for access permission
		check('Members');

		// Load the validation library
		$this->load->library('form_validation');
        $this->load->model('website_model');

		log_message('debug','Website admin class loaded');
	}

	/**
	 * View Members
	 *
	 * @access public
	 */
	function index()
	{
        $data['websites'] = $this->website_model->getWebsites();        
		$data['header'] = "Manage Websites";
		$data['page'] = "admin/website/websites_list";
		$data['module'] = 'wabag';
		$this->load->view($this->_container,$data);
	}
    
    function view(){
        die('view page');
    }
    
    function create(){
        if($_POST){            
            $item['website_name'] =  $this->input->post('name');
            $item['website_is_default'] = $this->input->post("default");
            $item['website_title'] = $this->input->post("title");
            $item['website_meta_tags'] = $this->input->post("meta_tags");
            $item['website_meta_description'] = $this->input->post("meta_description");
            $item['website_keywords'] = $this->input->post("keywords");
            $item['website_domain_postfix'] = $this->input->post("domain_postfix");
            $item['website_db_table_prefix'] = $this->input->post("table_prefix");
            $item['website_is_master'] = $this->input->post("master");
            $item['website_status'] = $this->input->post("status");
            $config = array(
               array(
                     'field'   => 'name', 
                     'label'   => 'Name', 
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'default', 
                     'label'   => 'Is Default', 
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'title', 
                     'label'   => 'Title', 
                     'rules'   => 'required'
                  ),            
               array(
                     'field'   => 'domain_postfix', 
                     'label'   => 'Domain Prefix', 
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'table_prefix', 
                     'label'   => 'Name', 
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'master', 
                     'label'   => 'Is Master', 
                     'rules'   => 'required|integer'
                  ),
               array(
                     'field'   => 'status', 
                     'label'   => 'Status', 
                     'rules'   => 'required|integer'
                  ),
            );

            $this->form_validation->set_rules($config);
            if ($this->form_validation->run() == FALSE)
            {
                //$this->form_validation->set_error_delimiters('<li>','</li>');
                flashMsg('error', $this->form_validation->error_string());
            }
            else
            {
                if($this->website_model->insert('website',$item))
                    flashMsg('success','The data has been saved');
                else
                    flashMsg('error','Unable to save your data. Please try again later');                
            }
        }
        
        //$data['websites'] = $this;
        $data['header'] = 'Add New Website';
        $data['page'] = 'admin/website/create_form';
        $data['module'] = 'wabag';
        $this->load->view($this->_container,$data);
    }
    
}
